package com.chat.datea.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chat.datea.R;
import com.chat.datea.bean.RecomTopViewPagerEntivity;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by dllo on 15/10/21.
 */
public class MusicRecomFragment_ViewPager_Adapter extends PagerAdapter {
    private Context context;
    private View view;
    private List<RecomTopViewPagerEntivity.PicEntity> pic;
    private ImageView imageView;

    public MusicRecomFragment_ViewPager_Adapter(Context context, List<RecomTopViewPagerEntivity.PicEntity> pic) {
        this.context = context;
        this.pic = pic;

    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        view = LayoutInflater.from(context).inflate(R.layout.recom_viewpager_img, null);
        imageView = (ImageView) view.findViewById(R.id.img);
        String url = pic.get(position % pic.size()).getRandpic();
        if (url != null) {
            Picasso.with(context).load(url).into(imageView);
        } else {
            imageView.setImageResource(R.mipmap.default_recommend);
        }
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
